package com.baidu.util;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpResponse;
@SuppressWarnings({"all"})
public class AuthCodeUtil {
	/**
	*@Description:  获取验证码方法
	*@Author: 王雨超
	*@Date: 2021/12/13
	*@Param:  手机号 验证码长度
	*@return: 验证码
	*/
	public static String getCode(String content, Integer size) {
		char[] charArray = content.toCharArray();
		for (int j = charArray.length-1; j>=0; j--) {
			Character c = charArray[j];
			if("-".equals(c.toString())) {
				char q = charArray[j-1];
				char h = charArray[j+1];
				String cont = "";
				
				int i = 0, k = h-q-1;
				for(i=q+1;i<h;i++) {
					cont += (char)i;
				}
				String qq = content.substring(0,j);
				String hh = content.substring(j+1);
				content = qq+cont+hh;
				charArray = content.toCharArray();
				j = j+k;
			}
		}
		String randomcode = "";
		// 用字符数组的方式随机
		String model = content;
		char[] m = model.toCharArray();
		for (int j = 0; j <  size; j++) {
			char c = m[(int) (Math.random() * content.length())];
			// 保证六位随机数之间没有重复的
			if (randomcode.contains(String.valueOf(c))) {
				j--;
				continue;
			}
			randomcode = randomcode + c;
		}
		return randomcode;
	}
	/**
	*@Description: 发送短信方法
	*@Author: 王雨超
	*@Date: 2021/12/13
	*@Param: map
	*@return: void
	*/
	public static boolean sendMsgToPhone(String phone,String code1,String register) {
		String host = "https://gyytz.market.alicloudapi.com";
		String path = "/sms/smsSend";
		String method = "POST";
		String appcode = "2e6df3f03501486793943a1d32269860";//你自己的AppCode
		Map<String, String> headers = new HashMap<String, String>();
		// 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("mobile", phone); //发送的手机号
		querys.put("param", "**code**:"+code1+",**minute**:1"); //发送的内容
		querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2"); //签名Id
		querys.put("templateId", register);//模板Id
		Map<String, String> bodys = new HashMap<String, String>();
		try {
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public static boolean sendMsgsToPhone(String phone,String message,String register) {
		String host = "https://gyytz.market.alicloudapi.com";
		String path = "/sms/smsSend";
		String method = "POST";
		String appcode = "2e6df3f03501486793943a1d32269860";//你自己的AppCode
		Map<String, String> headers = new HashMap<String, String>();
		// 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("mobile", phone); //发送的手机号
		querys.put("param", message); //发送的内容
		querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2"); //签名Id
		querys.put("templateId", register);//模板Id
		Map<String, String> bodys = new HashMap<String, String>();
		try {
			HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}